package com.koron.standard.jobStock.overhaul.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.koron.common.core.business.datadict.utils.DataDictTool;
import com.koron.standard.deviceClasssify.service.TypeService;
import com.koron.standard.jobStock.excel.JobStockListener;
import com.koron.standard.jobStock.overhaul.bean.dto.OverhaulDTO;
import com.koron.standard.jobStock.overhaul.bean.dto.OverhaulItemDTO;
import com.koron.standard.jobStock.overhaul.bean.po.OverhaulItemParams;
import com.koron.standard.jobStock.overhaul.bean.po.OverhaulReady;
import com.koron.standard.jobStock.overhaul.service.OverhaulService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/2/23
 */
@Slf4j
public class OverhaulListener extends JobStockListener {

    private OverhaulService overhaulService;
    private OverhaulDTO overhaulDTO;
    private boolean isItem;
    private boolean isReadies;
    private Map<String, String> majorCode;
    private Map<String, String> scaleCode;

    public OverhaulListener(TypeService typeService, OverhaulService overhaulService) {
        super(typeService);
        this.overhaulService = overhaulService;
        majorCode = DataDictTool.getDataDictByCode("publicDictionary", "common_major");
        majorCode = majorCode.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
        scaleCode = DataDictTool.getDataDictByCode("standardManagement", "standard_job_overhaul_scale");
        scaleCode = scaleCode.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
    }

    public String saveData() {
        if (overhaulDTO != null) {
            overhaulService.saveDto(overhaulDTO);
            return overhaulDTO.getId();
        }
        return null;
    }

    public void resetData() {
        isItem = false;
        isReadies = false;
        overhaulDTO = new OverhaulDTO();
        overhaulDTO.setItemDTOs(new ArrayList<>());
        overhaulDTO.setOverhaulReadies(new ArrayList<>());
    }

    @Override
    public String templateSheetName() {
        return "大修标准模板";
    }

    @Override
    public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
        if (isSkipped()) {
            return;
        }
        if ("作业项目".equals(map.get(0))) {
            isReadies = false;
            isItem = true;
            return;
        }
        if ("作业准备".equals(map.get(0))) {
            isReadies = true;
            isItem = false;
            return;
        }
        if ("作业标准名称".equals(map.get(0))) {
            // 作业标准名称
            if (StringUtils.isEmpty(map.get(2))) {
                setErrorMessage(getCurrentSheetName()+ "的作业标准名称不能为空");
                throw new ExcelAnalysisStopException();
            }
            overhaulDTO.setName(map.get(2));
            // 设备分类ID
            if (StringUtils.isEmpty(map.get(4))) {
                setErrorMessage(getCurrentSheetName()+ "的设备分类ID不能为空");
                throw new ExcelAnalysisStopException();
            }
            overhaulDTO.setTypeId(map.get(4));
            // 作业步骤层数
            overhaulDTO.setStepLevel(Integer.valueOf(map.get(6)));
            overhaulDTO.setAppid(getAppId());
            overhaulDTO.setStatus(0);
            overhaulDTO.setPublicStatus(0);
        } else if ("专业".equals(map.get(0))) {
            // 专业
            overhaulDTO.setMajor(majorCode.get(map.get(2)));
            // 检修规模
            overhaulDTO.setScale(scaleCode.get(map.get(4)));
        } else if (isReadies && map.get(1) != null && !"序号".equals(map.get(1))) {
            List<OverhaulReady> overhaulReadies = overhaulDTO.getOverhaulReadies();
            OverhaulReady overhaulReady = new OverhaulReady();
            if (StringUtils.isEmpty(map.get(2)) || StringUtils.isEmpty(map.get(3))) {
                setErrorMessage(getCurrentSheetName()+ "的作业准备不能为空");
                throw new ExcelAnalysisStopException();
            }
            overhaulReady.setType(map.get(2));
            overhaulReady.setTools(map.get(3));
            overhaulReadies.add(overhaulReady);
        } else if (isItem && map.get(1) != null && !"序号".equals(map.get(1))) {
            // 没有.则在第一层
            if (!map.get(1).contains(".")) {
                // 加入最外层
                OverhaulItemDTO overhaulItemDTO = getByMap(map);
                if (overhaulDTO.getStepLevel() > 1) {
                    overhaulItemDTO.setChildren(new ArrayList<>());
                }
                overhaulDTO.getItemDTOs().add(overhaulItemDTO);
            } else {
                // 长度为3则在第二层
                List<OverhaulItemDTO> itemDTOs = overhaulDTO.getItemDTOs();
                // 获取最后一层
                OverhaulItemDTO itemDTO = itemDTOs.get(itemDTOs.size() - 1);
                if (CollectionUtils.isEmpty(itemDTO.getChildren())) {
                    itemDTO.setChildren(new ArrayList<>());
                }
                itemDTO.getChildren().add(getByMap(map));
            }
        }

    }

    private List<OverhaulItemParams> resolveItemParams(String str) {
        List<String[]> strings = resolveItemStr(str, "\n");
        return strings.stream().map(e -> {
            OverhaulItemParams overhaulItemParams = new OverhaulItemParams();
            overhaulItemParams.setName(e[0]);
            overhaulItemParams.setUnit(e[1]);
            return overhaulItemParams;
        }).collect(Collectors.toList());
    }

    private OverhaulItemDTO getByMap(Map<Integer, String> map) {
        OverhaulItemDTO overhaulItemDTO = new OverhaulItemDTO();
        overhaulItemDTO.setContext(map.get(2));
        overhaulItemDTO.setContextStandard(map.get(3));
        overhaulItemDTO.setRisk(map.get(4));
        overhaulItemDTO.setLevel(getLevel(map.get(6)));
        overhaulItemDTO.setItemParams(resolveItemParams(map.get(5)));
        return overhaulItemDTO;
    }

}
